package com.steadyoung.modelapp.util.statusbar;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.view.Window;
import android.view.WindowManager;

import com.readystatesoftware.systembartint.SystemBarTintManager;

/**
 * Created by Administrator on 2017/11/29.
 * Email : steadyoung@foxmail.com
 * Auth  : wayne
 * Desc  : TODO
 */
public class SystemBarTintUtil {

    /**
     android:fitsSystemWindows="true"
     android:clipToPadding="false"
     在设置的activity布局中添加这两句才会使状态栏在屏幕顶部
     */


    /***
     * 自定义状态栏颜色
     * @param tintColor Color.parseColor("#24b7a4") //
     */
    public static void setTintColor(Activity activity,int tintColor){
        SystemBarTintManager tintManager = getSystemBarTintManager(activity);
        // 自定义颜色
        tintManager.setTintColor(tintColor);
    }

    /***
     * 自定义状态栏颜色
     * @param tintResource R.drawable.my_tint 资源id
     */
    public static void setTintResource(Activity activity,int tintResource){

        SystemBarTintManager tintManager = getSystemBarTintManager(activity);
        // 自定义状态栏颜色
        tintManager.setStatusBarTintDrawable(activity.getResources().getDrawable(tintResource));
    }

    /***
     * 自定义状态栏颜色
     * @param tintResource R.drawable.my_tint 资源id
     */
    public static void setNavigationBarTintResource(Activity activity,int tintResource){
        SystemBarTintManager tintManager = getSystemBarTintManager(activity);
        // 自定义状态栏颜色
        tintManager.setNavigationBarTintResource(tintResource);
    }

    /***
     * 自定义状态栏颜色
     * @param tintDrawable Drawable图片对象
     */
    public static void setTintDrawable(Activity activity,Drawable tintDrawable){
        SystemBarTintManager tintManager = getSystemBarTintManager(activity);
        // 自定义状态栏图片
        tintManager.setStatusBarTintDrawable(tintDrawable);
    }

    @NonNull
    private static SystemBarTintManager getSystemBarTintManager(Activity activity) {
        // 4.4及以上版本开启
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(activity,true);
        }

        SystemBarTintManager tintManager = new SystemBarTintManager(activity);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setNavigationBarTintEnabled(true);
        return tintManager;
    }

    @TargetApi(19)
    private static void setTranslucentStatus(Activity activity,boolean on) {
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }
}

